<?php
declare(strict_types=1);

namespace Zms\Unit;

/**
 * 表单数据助手
 * 处理的数据值为value  名称为label   下级为children
 */
class Tree
{

    /**
     * @describe 获取无极限数据所有相关层级
     * @user zlf <zms-pro@qq.com> 2025/10/22
     * @param $data
     * @param $value
     * @param $parentKey
     * @param $valueKey
     * @return array
     */
    public static function getFull($data, $value, $parentKey = 'pid', $valueKey = 'id'): array
    {
        $data = self::unpack($data);//解包图片

        //转为map
        $map = Arr::map($data, $valueKey, null);

        $fulls = [];

        while ($value) {
            $data = $map[$value] ?? null;
            if ($data) {
                $fulls[] = $data;
                $parentValue = $data[$parentKey] ?? null;
                if ($parentValue) {
                    $value = $parentValue;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return array_reverse($fulls);
    }


    /**
     * @describe 获取无极限数据所有相关值
     * @user zlf <zms-pro@qq.com> 2025/10/22
     * @param $data
     * @param $value
     * @param $parentKey
     * @param $valueKey
     * @return array
     */
    public static function getFullValue($data, $value, $parentKey = 'pid', $valueKey = 'id'): array
    {
        $data = self::unpack($data);//解包图片

        //转为map
        $map = Arr::map($data, $valueKey, null);

        $values = [];

        while ($value) {
            $data = $map[$value] ?? null;
            if ($data) {
                $values[] = $data[$valueKey];
                $parentValue = $data[$parentKey] ?? null;
                if ($parentValue) {
                    $value = $parentValue;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return array_reverse($values);
    }


    /**
     * @describe 获取无极限数据所有相关名称
     * @user zlf <zms-pro@qq.com> 2025/10/22
     * @param $data
     * @param $value
     * @param $parentKey
     * @param $valueKey
     * @param $labelKey
     * @return array
     */
    public static function getFullLabel($data, $value, $parentKey = 'pid', $valueKey = 'id', $labelKey = 'label'): array
    {
        $data = self::unpack($data);//解包图片
        //转为map
        $map = Arr::map($data, $valueKey, null);

        $labels = [];
        while ($value) {
            $data = $map[$value] ?? null;
            if ($data) {
                $labels[] = $data[$labelKey];
                $parentValue = $data[$parentKey] ?? null;
                if ($parentValue) {
                    $value = $parentValue;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return array_reverse($labels);
    }


    /**
     * 多维数组数据解包，下级键名必须是children
     * @param array $data
     * @param array $unpack
     * @param string $childrenKey
     * @return array
     */
    public static function unpack(array $data, array $unpack = [], string $childrenKey = 'children'): array
    {
        if (Is::list($data)) {//是列表
            foreach ($data as $row) {
                if (isset($row[$childrenKey]) && count($row[$childrenKey]) > 0) {
                    $children = $row[$childrenKey];
                    unset($row[$childrenKey]);
                    $unpack[] = $row;
                    $unpack = self::unpack($children, $unpack);
                } else {
                    unset($row[$childrenKey]);
                    $unpack[] = $row;
                }
            }
        } elseif (isset($data[$childrenKey]) && count($data[$childrenKey]) > 0) {
            $children = $data[$childrenKey];
            unset($data[$childrenKey]);
            $unpack[] = $data;
            $unpack = self::unpack($children, $unpack, $childrenKey);
        }
        return $unpack;
    }
}